layui.use(['layer', 'table', 'admin'], function () {
    var $ = layui.$;
    var table = layui.table;
    var admin = layui.admin;

    var Data = {
        tableId: "recordTable",    //表格id
        condition: {
            name: ""
        }
    };
    /**
     * 初始化表格的列
     */
    Data.initColumn = function () {
        return [[
            {field: 'contestId', title: '赛事号'},
            {field: 'contestName', title: '赛事名称'},
            {field: 'childName', title: '儿童姓名'},
            {
                field: 'childGender', title: '性别',
                templet: function (d) {
                    return d.childGender == 'M' ? '男' : d.childGender == 'F' ? '女' : '未知';
                }
            },
            {field: 'childBirthdate', title: '出生日期', minWidth: 170},
            {field: 'parentName', title: '家长姓名'},
            {field: 'parentPhone', title: '联系电话', minWidth: 90},
            {field: 'simpleName', title: '代理商'},
            {field: 'agentName', title: '代理人'},
            {field: 'createdTime', sort: true, title: '报名时间', minWidth: 170},
            {align: 'center', title: '照片详情', toolbar: '#titleTpl'}
        ]];
    };

    Data.photoUrlDetail = function (data) {
        if (data.photoUrl != null && data.photoUrl) {
            var arr = data.photoUrl.split(',');
            var detail = [];
            for (var i = 0; i < arr.length; i++) {
                detail.push({
                    "alt": i,
                    "pid": i,
                    "src": "http://childphoto-1258443025.picsh.myqcloud.com/" + arr[i] + "?imageView2/rq/70",
                    "thumb": ""
                })
            }
            var p = {
                "title": "", //相册标题
                "id": 1231, //相册id
                "start": 0, //初始显示的图片序号，默认0
                "data": detail
            }
            layer.photos({
                photos: p
                , anim: 5
            });
        }
    }
    Data.openAddcontest = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加',
            content: Feng.ctxPath + '/record/record_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Data.tableId);
            }
        });
    };
    /**
     * 点击查询按钮
     */
    Data.search = function () {
        var queryData = {conditions: $('#conditions').val()};
        table.reload(Data.tableId, {where: queryData});
    };
    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Data.openAddcontest();
    });

    /**
     * 导出excel按钮
     */
    Data.exportExcel = function () {
        var queryData = $('#conditions').val();
        if (queryData && queryData != '') {
            window.location.href = "/contest/record/excel?conditions=" + queryData;
        } else {
            window.location.href = "/contest/record/excel";
        }
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Data.tableId,
        url: Feng.ctxPath + '/record/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Data.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Data.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Data.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Data.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'photoUrlDetail') {
            Data.photoUrlDetail(data);
        }
    });
});
